//
// Created by liushuai on 18-8-5.
//

#ifndef EX7_STACK_HPP
#define EX7_STACK_HPP

#include <iostream>

template <typename T>
class Stack{
private:
    int mSize;
    int length;
    T *stack;
public:
    Stack();
    Stack(int cap);
    ~Stack();
    void clear();
    bool push(const T item);
    bool pop(T& item);
    bool top(T& item);
    bool isEmpty();
    bool isFull();
};
template <typename T>
Stack<T>::Stack():mSize(30),length(-1){
    stack = new T[mSize];
};
template <typename T>
Stack<T>::Stack(int cap):mSize(cap),length(-1){
    stack = new T[mSize];
};
template <typename T>
Stack<T>::~Stack() {
    delete [] stack;
    length = -1;
}
template <typename T>
void Stack<T>::clear() {
    length = -1;

}
template <typename T>
bool Stack<T>::push(const T item) {
    if(length == mSize-1)
    {
        std::cerr<<"栈已满，无法插入新数据"<<std::endl;
        return false;
    }
    else
    {
        stack[++length] = item;
        return true;
    }
}
template <typename T>
bool Stack<T>::pop(T &item) {
    if(length == -1)
    {
        std::cerr<<"无法从空栈获取元素"<<std::endl;
        return false;
    } else
    {
        item = stack[length--];
        return true;
    }
}
template <typename T>
bool Stack<T>::top(T &item) {
    if(length == -1)
    {
        std::cerr<<"无法从空栈中获取元素"<<std::endl;
        return false;
    }
    else
    {
        item = stack[length];
        return true;
    }
}
template <typename T>
bool Stack<T>::isEmpty() {
    if(length==-1)
        return true;
    else
        return false;
}
template <typename T>
bool Stack<T>::isFull() {
    if(length == mSize-1)
        return true;
    else
        return false;
}
#endif //EX7_STACK_HPP
